from models import db

from datetime import datetime

class User(db.Model):
    __tablename__ = 'tb_user'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True,doc='用户ID')
    mobile = db.Column(db.String(32), doc='手机号')
    password = db.Column(db.String(256), doc='密码')
    password2 = db.Column(db.String(256), doc='确认密码')
    nick_name = db.Column(db.String(32), doc='昵称')
    gender = db.Column(db.Integer, default=1, doc='1男0女')
    profile_photo = db.Column(db.String(256), nullable=True,doc='头像')
    created_time = db.Column(db.DateTime, default=datetime.now(),doc="创建时间")
    # last_login = db.Column(db.DateTime,default=datetime.now(), doc='最后登录时间')
    # is_media = db.Column(db.Boolean, default=False, doc='是否是自媒体')
    # is_verified = db.Column(db.Boolean, default=False, doc='是否实名认证')
    # introduction = db.Column(db.String(128),default='这个人很懒,什么都没有留下', doc='简介')
    # certificate = db.Column(db.String(64), default='已认证' ,doc='认证')
    # article_count = db.Column(db.Integer, default=0, doc='发帖数')
    # following_count = db.Column(db.Integer, default=0, doc='关注的人数')
    # fans_count = db.Column(db.Integer, default=0, doc='被关注的人数（粉丝数）')
    # like_count = db.Column(db.Integer, default=0, doc='累计点赞人数')
    # read_count = db.Column(db.Integer, default=0, doc='累计阅读人数')

    username = db.Column(db.String(32), doc='账号', unique=True)
    email = db.Column(db.String(64),nullable=True, doc='邮箱')
    # status = db.Column(db.Integer, default=1, doc='状态，1可用0不可用是否可用')

    is_superuser = db.Column(db.Integer, default=0, doc='0普通用户 1 管理员 2 超级管理员')
    vip = db.relationship('VipModel', backref='vip_user')
    # news = db.relationship('News', backref="news", uselist=False)
    # comment = db.relationship('Comment', backref=db.backref('comments'), uselist=False)
    # search = db.relationship('SearchHistory', backref=db.backref('searches'), uselist=False)


# 书
class Books(db.Model):
     __tablename__ = "tb_books"
     id = db.Column(db.Integer, primary_key=True, autoincrement=True, doc='书的id')
     book_name = db.Column(db.String(50),doc='书名')
     book_auther = db.Column(db.Integer, db.ForeignKey('tb_auther.id'), doc='所属作者')
     book_publicate_time = db.Column(db.DateTime,default=datetime.now(), doc="创建时间")
     book_price = db.Column(db.Float,doc='价格')

# 作者
class Auther(db.Model):
    __tablename__ = 'tb_auther'
    id = db.Column(db.Integer, primary_key=True, doc='作者id')
    name = db.Column(db.String(32), doc='作者姓名')
    gender = db.Column(db.String(20), doc='性别')
    pen_name = db.Column(db.String(32), doc='笔名')
    books_id = db.relationship('Books', backref='books')


class OAuthUserModel(db.Model):
    '''
    三方登录的表，记录当前三方登录和用户的关系
    '''
    __tablename__ = 'oauth_user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True ,doc='oauth_user id')
    image = db.Column(db.String(256), doc='头像')
    oauth_id = db.Column(db.String(512), doc='第三方登录的id')
    user = db.Column(db.Integer, db.ForeignKey("tb_user.id", ondelete="CASCADE"))
    oauth_type = db.Column(db.String(128), doc='第三方登录类型')


class VipModel(db.Model):
    """vip表"""
    __tablename__ = 'tb_vip'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(128), doc='vip名称')
    # level = db.Column(db.Integer, default=0, doc='vip等级(0普通用户,1普通会员2高级会员)')
    level = db.Column(db.Integer, db.ForeignKey('tb_role.id', ondelete="CASCADE"), doc="vip等级")
    price = db.Column(db.DECIMAL(13, 2), doc='会员价格')
    desc = db.Column(db.String(64), doc='vip描述')
    period = db.Column(db.Integer, default=365, doc='vip有效期')
    exempt_cour = db.Column(db.Integer, doc='免费课程{0或空:不享受,1:享受}')
    vip_cour = db.Column(db.Integer, doc='会员课程{0或空:不享受,1:享受}')
    environment = db.Column(db.Integer, doc='实验环境联网{0或空:不享受,1:享受}')
    save = db.Column(db.Integer, doc='保存2个环境(30天){0或空:不享受,1:享受}')
    client = db.Column(db.Integer, doc='客户端{0或空:不享受,1:享受}')
    ssh = db.Column(db.Integer, doc='SSH直连{0或空:不享受,1:享受}')
    web_ide = db.Column(db.Integer, doc='WebIDE {0或空:不享受,1:享受}')
    discounts = db.Column(db.Integer, doc='训练营优惠{0或空:不享受,1:享受}')
    exempt_study = db.Column(db.Integer, doc='训练营课程免费学习{0或空:不享受,1:享受}')
    user = db.Column(db.Integer, db.ForeignKey('tb_user.id', ondelete="CASCADE"))
